<?php
/**
 * @title 2477. 到达首都的最少油耗 - 错误
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $roads
     * @param Integer $seats
     * @return Integer
     */
    function minimumFuelCost($roads, $seats) {
        /**
         * @since 2023-12-05 总耗油量
         */
        $totalFuelCost = 0;

        /**
         * @since 2023-12-05 哈希建立城市到城市之间关系，遍于查找
         */
        $cityToCityArray = [];
        foreach ($roads as list($city1, $city2)){
            $cityToCityArray[$city1][$city2] = $city2;
            $cityToCityArray[$city2][$city1] = $city1;
        }

        /**
         * @since 2023-12-05 从首都城市（根节点）出发，层次遍历
         *
         *        0
         *      / | \
         *     1  4  5
         *    /   |
         *   3    6
         *  /
         * 2
         *
         * @since 2023-12-05 城市之间耗油量一致，计算一个等差数列，差值座位数$seats
         */
        $cityArray = [0];
        $distance = 0;
        while($cityArray){
            /**
             * @since 2023-12-05 下次遍历的城市集合
             */
            $nextCityArray = [];

            /**
             * @since 2023-12-05 遍历当前城市集合
             */
            foreach ($cityArray as $city1){
                /**
                 * @since 2023-12-05 这条路径走到终点
                 */
                if(empty($cityToCityArray[$city1])){
                    /**
                     * @since 2023-12-05 计算这条路径的耗油量
                     * @since 2023-12-05 最低耗油量
                     * 等差数列
                     * 最大数 = 最大耗油量
                     * 最小数 = 最小耗油量
                     * 差值 = 车子数量
                     */
                    if($distance <= $seats){
                        /**
                         * @since 2023-12-05 一辆车解决
                         */
                        $pathFuelCost = $distance;
                    } else {
                        /**
                         * @since 2023-12-05 起点耗油量
                         */
                        $costMin = $distance % $seats;

                        /**
                         * @since 2023-12-05 需要车子数量
                         */
                        $carCnt = intval($distance/$seats)+1;

                        /**
                         * @since 2023-12-05 这条路径总耗油量
                         * a1*n + n*(n-1)*d/2
                         */
                        $pathFuelCost = $costMin*$carCnt + (($carCnt*($carCnt-1)*$seats)>>1);
                    }
                    $totalFuelCost += $pathFuelCost;
                    echo "终点{$city1}, 距离首都{$distance}, 这条路径耗油{$pathFuelCost}", PHP_EOL;
                } else {
                    foreach ($cityToCityArray[$city1] as $city2){
                        $nextCityArray[] = $city2;

                        /**
                         * @since 2023-12-05 规避回头路
                         */
                        unset($cityToCityArray[$city2][$city1]);
                    }
                }
            }

            /**
             * @since 2023-12-05 赋值下次遍历的城市集合
             */
            $cityArray = $nextCityArray;

            /**
             * @since 2023-12-05 距离+1
             */
            $distance++;
        }

        /**
         * @return
         */
        return $totalFuelCost;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.05.php
 */
$datas = [
    // [[[0,1],[0,2],[0,3]], 5], // 3
    // [[[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], 2], // 7
    [[[0,1],[0,2],[1,3],[1,4]], 5], // 4
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minimumFuelCost(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
